<script lang="ts" setup name="XtxCity">
import { ref } from 'vue';
import {onClickOutside} from '@vueuse/core'
import {AreaList,CityResult} from '@/types/good'
import axios from 'axios'
import { watch } from 'vue';
const active = ref(false)
const cityList = ref<AreaList[]>([])
// 原始数据
const cacheList = ref<AreaList[]>([])
const changeResult = ref<CityResult>({
  provinceCode: '',
  provinceName: '',
  cityCode: '',
  cityName: '',
  countyCode: '',
  countyName: ''
})
const toggle = ()=>{
  active.value = !active.value
}
const target = ref(null)
onClickOutside(target,()=>{
  active.value = false
})
// 城市列表
const getCityList =async ()=>{
  const res = await axios.get<AreaList[]>(
    'https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json'
  )
  cityList.value = res.data
  // 赋值给原始数据
  cacheList.value = res.data
}
getCityList()
// 子传父
const emit = defineEmits<{
  (e:'changeCity',value:CityResult):void}>()
// 点击城市的选择
const selectCity = (city:AreaList)=>{
  if(city.level===0){
    cityList.value = city.areaList
    changeResult.value.provinceName = city.name
    changeResult.value.provinceCode = city.code

  }
  if(city.level===1){
    cityList.value = city.areaList
    changeResult.value.cityName = city.name
    changeResult.value.cityCode = city.code
  }
  if(city.level===2){
    // 最后一层就关闭弹层
    active.value = false
    changeResult.value.countyName = city.name
    changeResult.value.countyCode = city.code
    emit('changeCity',changeResult.value)
  }
}
// 监听弹层是关闭  关闭就恢复原始数据
watch(active,(newValue)=>{
  if(!newValue){
    cityList.value = cacheList.value
  }
})
defineProps<{
  userAddress?:string
}>()
</script>
<template>
  <div class="xtx-city" ref="target">
    <div class="select" @click="toggle" :class="{active:active}">
    <span class="value" v-if="userAddress">{{userAddress}}</span>
      <span class="placeholder" v-else>请选择配送地址</span>
      <span class="value"></span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-show="active">
      <span class="ellipsis" v-for="item in cityList" :key="item.code" @click="selectCity(item)">{{item.name}}</span>
    </div>
  </div>
</template>

<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  margin-left: 5px;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>